<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<head>
    <title>社交平台</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/css/style.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
</head>
<jsp:include page="/views/header.jsp"/>

<div class="container mt-4">
    <div class="row">
        <div class="col-md-3">
            <div class="card">
                <div class="card-header">
                    <h5>好友管理</h5>
                </div>
                <div class="card-body">
                    <ul class="nav flex-column">
                        <li class="nav-item">
                            <a class="nav-link" href="${pageContext.request.contextPath}/friend/list">好友列表</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="${pageContext.request.contextPath}/friend/requests">好友请求</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link active" href="${pageContext.request.contextPath}/friend/search">查找好友</a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
        <div class="col-md-9">
            <div class="card">
                <div class="card-header">
                    <h5>查找好友</h5>
                </div>
                <div class="card-body">
                    <c:if test="${not empty message}">
                        <div class="alert alert-success">${message}</div>
                    </c:if>
                    <c:if test="${not empty error}">
                        <div class="alert alert-danger">${error}</div>
                    </c:if>

                    <form action="${pageContext.request.contextPath}/friend/search" method="get" class="mb-4">
                        <div class="input-group">
                            <input type="text" class="form-control" name="keyword" placeholder="输入昵称或用户ID" value="${param.keyword}">
                            <button class="btn btn-primary" type="submit">搜索</button>
                        </div>
                    </form>

                    <c:if test="${not empty keyword}">
                        <h6 class="mb-3">搜索结果: "${keyword}"</h6>
                    </c:if>

                    <c:if test="${not empty searchResults}">
                        <div class="list-group">
                            <c:forEach items="${searchResults}" var="user">
                                <div class="list-group-item">
                                    <div class="d-flex justify-content-between align-items-center">
                                        <div class="d-flex align-items-center">
                                            <img src="${pageContext.request.contextPath}${not empty user.avatar ? user.avatar : '/images/default-avatar.png'}"
                                                 class="avatar-sm me-3" alt="用户头像">
                                            <div>
                                                <h6 class="mb-0">${user.nickname}</h6>
                                                <small class="text-muted">ID: ${user.userId}</small>
                                            </div>
                                        </div>
                                        <c:choose>
                                            <c:when test="${user.isFriend}">
                                                <span class="badge bg-success">已是好友</span>
                                            </c:when>
                                            <c:when test="${user.hasPendingRequest}">
                                                <span class="badge bg-warning text-dark">请求已发送</span>
                                            </c:when>
                                            <c:otherwise>
                                                <form action="${pageContext.request.contextPath}/friend/send" method="post">
                                                    <input type="hidden" name="friendId" value="${user.userId}">
                                                    <button type="submit" class="btn btn-sm btn-primary">添加好友</button>
                                                </form>
                                            </c:otherwise>
                                        </c:choose>
                                    </div>
                                </div>
                            </c:forEach>
                        </div>
                    </c:if>

                    <c:if test="${empty searchResults and not empty keyword}">
                        <div class="alert alert-info">没有找到匹配的用户</div>
                    </c:if>

                    <div class="card mt-4">
                        <div class="card-header">
                            <h6>推荐好友</h6>
                        </div>
                        <div class="card-body">
                            <c:if test="${empty recommendedFriends}">
                                <div class="alert alert-info">暂无推荐好友</div>
                            </c:if>
                            <c:if test="${not empty recommendedFriends}">
                                <div class="row">
                                    <c:forEach items="${recommendedFriends}" var="friend">
                                        <div class="col-md-6 mb-3">
                                            <div class="card">
                                                <div class="card-body">
                                                    <div class="d-flex justify-content-between align-items-center">
                                                        <div class="d-flex align-items-center">
                                                            <img src="${pageContext.request.contextPath}${not empty friend.avatar ? friend.avatar : '/images/default-avatar.png'}"
                                                                 class="avatar-sm me-3" alt="用户头像">
                                                            <div>
                                                                <h6 class="mb-0">${friend.nickname}</h6>
                                                                <small class="text-muted">${friend.mutualFriends} 位共同好友</small>
                                                            </div>
                                                        </div>
                                                        <form action="${pageContext.request.contextPath}/friend/send" method="post">
                                                            <input type="hidden" name="friendId" value="${friend.userId}">
                                                            <button type="submit" class="btn btn-sm btn-outline-primary">添加</button>
                                                        </form>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </c:forEach>
                                </div>
                            </c:if>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<jsp:include page="/views/footer.jsp"/>